import { type Plugin } from 'vue'
// import { Dialog } from 'vant'
import localVersion from '../../public/version.json'

let toggleDialog = false

const version = {
  install() {
    window.addEventListener(
      'click',
      async () => {
        if (toggleDialog) return
        const system = await (
          await fetch(
            `${
              import.meta.env.PROD ? `${import.meta.env.BASE_URL}/` : '/public/'
            }version.json?t=${new Date().getTime()}`
          )
        ).json()

        localStorage.setItem('newH5Version', String(localVersion.version))
        console.log('system', system, localVersion)

        if (!localVersion || Number(localVersion.version) !== Number(system.version)) {
          toggleDialog = true
          //
          // window.location.reload()
        }
      },
      true
    )
  }
} as Plugin

export default version
